import { Component } from 'react';
import React from 'react';
import { connect } from 'umi';
import HighPerformTable from '@/components/HighPerformTable';
import APIS from '@/apis';
import { Button, Input } from 'antd';
import { PlusOutlined } from '@ant-design/icons';
import SearchAbleSelect from '@/components/SearchAbleSelect';

class PermissionPage extends Component<any, any> {
  actionRef: any;

  constructor(props: any) {
    super(props);
  }

  handleDelete(data: any) {
    return APIS.UserAPI.DeletePermission(data.permissionId)
      .then(() => {
        return Promise.resolve(true);
      })
      .catch(() => {
        return Promise.resolve(false);
      });
  }

  handleAdd(data: any) {
    data.name = "ROLE_" + data.name;
    return APIS.UserAPI.CreateNewPermission({ ...data, desc: data.description })
      .then(() => {
        return Promise.resolve(true);
      })
      .catch(() => {
        return Promise.resolve(false);
      });
  }

  handleUpdate(data: any) {
    data.name = "ROLE_" + data.name;
    return APIS.UserAPI.UpdatePermission({ ...data, desc: data.description })
      .then(() => {
        return Promise.resolve(true);
      })
      .catch(() => {
        return Promise.resolve(false);
      });
  }
  handleChangeState(record: any, state: boolean) {
    return APIS.UserAPI.ChangePermissionState({ id: record.permissionId, state: state ? 1 : 0 }).then(
      (response) => {
        return Promise.resolve(response.data.state === 1);
      },
    );
  }
  requestGetData(
    params: {
      pageSize?: number;
      current?: number;
    },
    sorter: {
      [key: string]: 'ascend' | 'descend';
    },
    filter: {
      [key: string]: React.ReactText[];
    },
  ) {
    return APIS.UserAPI.GetPermissionList({ ...params, sorter, filter });
  }
  clomns: any = [
    {
      title: '权限名称',
      dataIndex: 'name',
      rules: [
        {
          required: true,
          message: '权限名称为必填项',
        },
      ],
      renderFormItem:()=>{
        return <Input addonBefore="ROLE_"></Input>
      }
    },
    {
      title: '权限描述',
      dataIndex: 'description',
      valueType: 'textarea',
      rules: [
        {
          required: true,
          message: '权限描述为必填项',
        },
      ],
    },
    {
      title: '资源类型',
      dataIndex: 'resType',
      valueEnum: {
        0: { text: '未定义', status: 'Default' },
        1: { text: '接口', status: 'api' },
        2: { text: '页面', status: 'page' },
        3: { text: '按钮', status: 'button' },
      },
      rules: [
        {
          required: true,
          message: '资源类型为必填项',
        },
      ],
    },
    {
      title: '父级权限',
      dataIndex: 'parent',
      hideInTable:true,
      renderFormItem: (item,config,form) => <SearchAbleSelect name="parent" form={form} getValue={(a)=>console.log(a)}  searchData={(data)=>APIS.UserAPI.SearchPermission(data).then((data)=>data.map((item)=>{return {key:item.name+"-"+item.description,value:item.id}}))}></SearchAbleSelect>
    },
  ];
  render() {
    
    return (
      <div>
        {/* <KeywordFilterSearch
          filters={[
            { title: '角色类型', multiple: false, keywords: ['全部', '教务管理员', '教师'] },
          ]}
        ></KeywordFilterSearch> */}
        <HighPerformTable<any>
          requestGetData={this.requestGetData.bind(this)}
          ref={(ref) => (this.actionRef = ref)}
          columns={this.clomns}
          rowKey={'permissionId'}
          
          handleChangeState={this.handleChangeState.bind(this)}
          handleDelete={this.handleDelete.bind(this)}
          handleAdd={this.handleAdd.bind(this)}
          handleUpdate={this.handleUpdate.bind(this)}
          toolBarRender={() => [
            <Button
              key="new"
              type="primary"
              onClick={() => this.actionRef.handleModalVisible(true)}
            >
              <PlusOutlined /> 新增权限
            </Button>,
          ]}
        />
      </div>
    );
  }
}

export default connect()(PermissionPage);
